package text1;

import java.util.ArrayList;
import java.util.List;

class PersonalBillingManagement {
    private List<Bill> bills;  // 存储账单的列表
    private double budget;  // 预算

    public PersonalBillingManagement() {
        bills = new ArrayList<>();
        this.budget = 0;
    }

    // 记录收入的方法，添加对输入金额的校验
    public void recordIncome(double amount, String description) {
        if (amount >= 0) {
            Bill bill = new Bill("Income", amount, description);
            bills.add(bill);
            System.out.println("记录收入: " + amount + ", 描述: " + description);
        } else {
            System.out.println("输入的收入金额不合法，请输入非负金额。");
        }
    }

    // 记录支出的方法，添加对输入金额的校验
    public void recordExpense(double amount, String description) {
        if (amount >= 0) {
            Bill bill = new Bill("Expense", amount, description);
            bills.add(bill);
            System.out.println("记录支出: " + amount + ", 描述: " + description);
        } else {
            System.out.println("输入的支出金额不合法，请输入非负金额。");
        }
    }

    // 查看所有账单的方法
    public void viewAllBills() {
        System.out.println("查看所有账单:");
        for (Bill bill : bills) {
            System.out.println(bill);
        }
    }

    // 查询账单的方法
    public void queryBill(String description) {
        System.out.println("查询账单，描述: " + description);
        for (Bill bill : bills) {
            if (bill.getDescription().equals(description)) {
                System.out.println(bill);
            }
        }
    }

    // 预算管理：设置预算
    public void setBudget(double budget) {
        this.budget = budget;
        System.out.println("设置预算为: " + budget);
    }

    // 月度统计：计算总收入和总支出，处理预算超支情况
    public void monthlyStatistics() {
        double totalIncome = 0;
        double totalExpense = 0;
        for (Bill bill : bills) {
            if (bill.getType().equals("Income")) {
                totalIncome += bill.getAmount();
            } else if (bill.getType().equals("Expense")) {
                totalExpense += bill.getAmount();
            }
        }
        System.out.println("月度统计:");
        System.out.println("总收入: " + totalIncome);
        System.out.println("总支出: " + totalExpense);
        System.out.println("预算: " + budget);
        double budgetBalance = budget - totalExpense;
        if (budgetBalance >= 0) {
            System.out.println("预算余额: " + budgetBalance);
        } else {
            System.out.println("预算已超支，超支金额: " + Math.abs(budgetBalance));
        }
    }
}